package com.solomiao.demo.multi.thread.demo2;

public class ThreadImplementDemo2 implements Runnable {

    private boolean isRunning = true;

    @Override
    public void run() {
        System.out.println("子线程：开始运行...");
        int count = 0;
        // 线程运行时，判断isRunning是否为true，如果为true，则继续运行线程
        while (isRunning) {
            try {
                System.out.println("子线程运行中... count = " + (++count));
                Thread.sleep(500); // 模拟工作
            } catch (InterruptedException e) {
                System.out.println("子线程被中断了");
                // 可以在这里根据需要处理中断，比如也退出循环
                break;
            }
        }
        System.out.println("子线程：已安全退出...");
    }

    /**
     * @desc 停止线程
     * */
    public void setRunning(boolean running) {
        isRunning = running;
    }

}
